package com.gitee.cirnochat.login.aop;

import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.login.annotation.TrackLogout;
import com.gitee.cirnochat.login.service.LoginDeviceService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@RequiredArgsConstructor
@Slf4j
public class UserLogoutInterceptor {
    private final LoginDeviceService loginDeviceService;
    private final HttpServletRequest request;

    /**
     * 登出成功后删除登陆设备
     */
    @AfterReturning("@annotation(trackLogout)")
    public void trackLoginSuccess(JoinPoint joinPoint, TrackLogout trackLogout) {
        Long userId = RequestUtils.getUserId(request);
        String deviceId = RequestUtils.getDeviceId(request);
        loginDeviceService.deleteLoginDeviceByDeviceIdAsync(deviceId,userId);
    }

}
